//
//  CustomerViewController.m
//  Spa
//
//  Created by PangPond on 10/1/2556.
//  Copyright (c) พ.ศ. 2556 innova. All rights reserved.
//

#import "CustomerViewController.h"
#import "AddCustomerViewController.h"
#import "Customer.h"
@interface CustomerViewController ()

@end

@implementation CustomerViewController
@synthesize customer = _customer;
@synthesize tableView = _tableView;
@synthesize keyDelete;
@synthesize databaseUtility =_databaseUtility;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self loadView];
    [self viewDidLoad];
}

- (BOOL) hidesBottomBarWhenPushed {
    return YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.hidesBottomBarWhenPushed = YES;
    self.databaseUtility = [[DatabaseUtility alloc]initWithDatabaseName:@"Database"];
    self.customer = [self.databaseUtility fetchedResultsWithClass:@"Customer"withSortDescriptorString:@"firstname"];
    [self.tableView reloadData];
    UIBarButtonItem * settingButton = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(AddCustomer:)];
    self.navigationItem.rightBarButtonItem = settingButton;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.customer count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //////
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    //config xell
    
    Customer * customers = [self.customer objectAtIndex:indexPath.row];
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@  %@",customers.firstname,customers.lastname];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AddCustomerViewController *addCustomer = [[AddCustomerViewController alloc] initWithNibName:@"AddCustomerViewController" bundle:nil];
    Customer * customers = [self.customer objectAtIndex:indexPath.row];
    addCustomer.keyCustomerFirstname = customers.firstname;
    addCustomer.keyCustomertLastname = customers.lastname;
    addCustomer.keyIf = @"1";
    [self presentViewController:addCustomer animated:YES completion:nil];
}


//funtion



-(IBAction)AddCustomer:(id)sender{
    AddCustomerViewController *addCustomer = [[AddCustomerViewController alloc] initWithNibName:@"AddCustomerViewController" bundle:nil];
    addCustomer.keyIf = @"2";
    [self presentViewController:addCustomer animated:YES completion:nil];
    
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    Customer * customerKey = [self.customer objectAtIndex:indexPath.row];
    keyDelete = customerKey.firstname;
    
    NSArray *customers = [self.databaseUtility fetchedResultsWithClass:@"Customer" withSortDescriptorString:@"firstname" withPredicateString:[NSString stringWithFormat:@"firstname == '%@'",keyDelete]];
    
    Customer *customer = [customers lastObject];
    [self.databaseUtility.managedObjectContext deleteObject:customer];
    [self.databaseUtility saveContext];
    [self viewDidLoad];
}
@end
